home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-05-26 | 5.2 KB | 213 lines | [TEXT/MPS ] |
- *---------------------------------------------------------------------------
- *
- * IMPLEMENTATION of UNIT Timing
- *
- * Version 1.0 / O. Maquelin / 22-May-89
- *
- * *** Runs only on Macintosh II ***
- *
- *---------------------------------------------------------------------------
-
- CASE ON
- MACHINE MC68020 ; needs 68020 instructions
-
- HWNonPortable EQU 1 ; needs Mac II hardware
- onMac EQU 0
- onNuMac EQU 1
-
-
- INCLUDE 'HardwareEqu.a'
- INCLUDE 'SysEqu.a'
-
-
- EXPORT (unitComp, totComp): DATA
- EXPORT (INITTIMER, STARTTIMER, STOPTIMER): CODE
-
-
- ClkPerTick EQU 13024 ; cycles per tick (16.663 ms)
-
-
- Timer RECORD 0 ; local definition of Timer
- hi DS.L 1 ; high longword
- lo DS.L 1 ; low longword
- ENDR
-
-
- *---------------------------------------------------------------------------
- *
- * Declaration of the exported variables
- *
- *---------------------------------------------------------------------------
-
- unitComp RECORD EXPORT ; 27 cycles compensation (35µs)
- DC.L 27
- ENDR
-
- totComp RECORD EXPORT ; totComp initially zero
- hi DC.L 0
- lo DC.L 0
- ENDR
-
-
- *---------------------------------------------------------------------------
- *
- * PROCEDURE InitTimer (VAR t: TimeRec)
- *
- * Initializes a timer (t := 0)
- *
- *---------------------------------------------------------------------------
-
- INITTIMER PROC EXPORT
-
- MOVE.L (SP)+,A0 ; get return address
- MOVE.L (SP)+,A1 ; get address of t
-
- CLR.L (A1)+ ; clear two longwords
- CLR.L (A1)
-
- JMP (A0) ; back to caller
- ENDPROC
-
-
- *---------------------------------------------------------------------------
- *
- * PROCEDURE GetTime (hi: D0.L; lo: D1.L)
- *
- * GetTime returns the actual time in clock cycles (1.2766 µs per
- * cycle) in the registers D0 and D1. Time is determined from the
- * global variable Ticks and from the state of VIA 2.
- *
- *---------------------------------------------------------------------------
-
- GetTime PROC ENTRY
-
- MOVE.L #VBase2,A1 ; get base address of VIA2
-
- MOVE SR,-(SP) ; disable interrupts
- ORI #$0700,SR
-
- MOVE.B vT1CH(A1),D1 ; read high byte of timer 1
- MOVE.B vBufB(A1),D0 ; read state of pseudo-VBL
- MOVE.B vT1C(A1),D2 ; read low byte of timer 1
- ROR.W #8,D2
- MOVE.B vT1CH(A1),D2 ; read high byte a second time
-
- CMP.B D1,D2 ; if both are equal we are done,
- BEQ.S @1 ; else read everything once more
-
- MOVE.B vBufB(A1),D0 ; read state of pseudo-VBL
- MOVE.B vT1C(A1),D2 ; read low byte of timer 1
- ROR.W #8,D2
- MOVE.B vT1CH(A1),D2 ; read high byte of timer 1
-
- @1 ROR.W #8,D2 ; exchange low and high byte
-
- MOVEQ #7,D1 ; first phase of the tick?
- BTST.L D1,D0
- BNE.S @2
- ADD.W #ClkPerTick/2,D2 ; no, correct number of cycles
-
- @2 MOVE.L Ticks,D1 ; read Ticks
-
- MOVE (SP)+,SR ; enable interrupts
-
- CMP.W #ClkPerTick-10,D2 ; was the value of Ticks valid?
- BLE.S @3
- ADDQ #1,D1 ; no, correct the value read
-
- @3 MULU.L #ClkPerTick,D0:D1 ; convert ticks to cycles and
- EXT.L D2 ; subtract VIA value
- SUB.L D2,D1
- MOVEQ #0,D2
- SUBX.L D2,D0
-
- RTS
- ENDPROC
-
-
- *---------------------------------------------------------------------------
- *
- * PROCEDURE StartTimer (VAR t: Timer)
- *
- * Starts a timer (t := t - Time + totComp;
- * totComp := totComp + unitComp)
- *
- *---------------------------------------------------------------------------
-
- STARTTIMER PROC EXPORT
-
- MOVEC CACR,D0 ; disable cache, save old state
- MOVE.L D0,A0
- AND.B #$FE,D0
- MOVEC D0,CACR
-
- MOVE.L unitComp,D0 ; increment total compensation
- ADD.L D0,totComp.lo
- BCC.S @1 ; is there a carry to add
- ADDQ #1,totComp.hi ; yes, increment high word
-
- @1 JSR GetTime ; determine actual time
-
- MOVE.L 4(SP),A1 ; get address of t
-
- MOVE.L totComp.hi,D2 ; subtract compensation
- SUB.L totComp.lo,D1
- SUBX.L D2,D0
-
- MOVE.L Timer.hi(A1),D2 ; subtract result from timer
- SUB.L D1,Timer.lo(A1)
- SUBX.L D0,D2
- MOVE.L D2,Timer.hi(A1)
-
- MOVEC A0,CACR ; restore cache state
-
- MOVE.L (SP)+,A0 ; return to caller
- ADDQ #4,SP
- JMP (A0)
- ENDPROC
-
-
- *---------------------------------------------------------------------------
- *
- * PROCEDURE StopTimer (VAR t: Timer)
- *
- * Stops a timer (t := t + Time - totComp;
- * totComp := totComp + unitComp)
- *
- *---------------------------------------------------------------------------
-
- STOPTIMER PROC EXPORT
-
- MOVEC CACR,D0 ; disable cache, save old state
- MOVE.L D0,A0
- AND.B #$FE,D0
- MOVEC D0,CACR
-
- MOVE.L unitComp,D0 ; increment total compensation
- ADD.L D0,totComp.lo
- BCC.S @1 ; is there a carry to add
- ADDQ #1,totComp.hi ; yes, increment high word
-
- @1 JSR GetTime ; determine actual time
-
- MOVE.L 4(SP),A1 ; get address of t
-
- MOVE.L totComp.hi,D2 ; subtract compensation
- SUB.L totComp.lo,D1
- SUBX.L D2,D0
-
- MOVE.L Timer.hi(A1),D2 ; add result to timer
- ADD.L D1,Timer.lo(A1)
- ADDX.L D0,D2
- MOVE.L D2,Timer.hi(A1)
-
- MOVEC A0,CACR ; restore cache state
-
- MOVE.L (SP)+,A0 ; return to caller
- ADDQ #4,SP
- JMP (A0)
- ENDPROC
-
-
- END
-